package com.yjhb.android.sbz.mvp.timerlist;

import com.yjhb.android.sbz.entity.Timer;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建者 zx
 * 创建时间 2021/1/13 11:11
 */
public class TimerListPresenter extends ConventionPresenter<TimerListView> {
    private final String TAG = "TimerListPresenter";

    @Override
    protected void onDestroy() {

    }

    public TimerListPresenter(TimerListView timerListView) {
        super(timerListView);
    }

    public void getListTimer(Integer taskId,Integer type, int size, int current) {
        HttpTool.request(model.getListTimer(taskId,type, size, current), new HttpObserver<List<Timer>>() {
            @Override
            public void onResult(List<Timer> data) {
                if (view != null) {
                    List<Timer> timers = new ArrayList<>();
                    for (Timer t : data
                    ) {
                        if (t.getState() != 2) {
                            timers.add(t);
                        }
                    }
                    view.onLoad(timers, current == 1);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(-2, msg);
                }
            }
        });
    }


    public void changeState(int index,Integer id, Integer state) {
        if (view != null) {
            view.onWait("处理中...");
        }
        HttpTool.request(model.updateTimerState(id, state), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onSuccess(index,state);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }
}
